<template>
  <!-- 新增部门的弹层 -->
  <el-dialog title="新增部门" :visible="showDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addDeptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getEmployeeSimple"
          @blur="checkManager"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small">确定</el-button>
        <el-button size="small">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    node: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    const checkRepeatName = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      // debugger
      const arr = depts.filter((item) => item.pid === this.node.id)
      console.log(arr)
      const isRepeat = arr.some((item) => item.name === value)
      isRepeat ? callback(new Error('同一部门不能重复')) : callback()
    }

    const checkRepeatCode = async(rule, val, callback) => {
      // 1. 获取全部部门
      const { depts } = await getDepartments()
      // 2. 判断是否有相同 code
      const isRepeat = depts.some((item) => item.code === val)
      // 3. 处理后续
      isRepeat ? callback(new Error('部门编码必须唯一')) : callback()
    }
    return {
      employees: [],
      // 定义表单数据
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1-50 之间', trigger: 'blur' },
          { trigger: 'blur', validator: checkRepeatName }
        ],
        code: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1-50 之间', trigger: 'blur' },
          { trigger: 'blur', validator: checkRepeatCode }
        ],
        manager: [{ required: true, message: '数据不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1-300 之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getEmployeeSimple() {
      this.employees = await getEmployeeSimple()
      console.log(this.employees)
    }
  },
  checkManager() {
    setTimeout(() => {
      this.$refs.addDeptForm.validateField('managervpxkn')
    }, 100)
  }
}
</script>

<style>
</style>
